﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cecbData;

namespace cecbClasses.Data_Adapters.ImpactAndActivity
{
    public class Func_Impacts
    {
        public Func_Impacts()
        {
            dataContext = new DSSEntities();
        }

        DSSEntities dataContext;


        public List<Impacts> getAllImpacts()
        {

            IQueryable<Impacts> Query =
                  from c in dataContext.Impacts
                  select c;

            List<Impacts> Impacts = Query.ToList();

            return Impacts;
        }

        public List<string> getComponentMainCat(string project)
        {
            List<string> complist = new List<string>();
            IQueryable<string> quary = (from p in dataContext.Components
                                        join b in dataContext.Impacts on p.cmpt_reference equals b.impt_component
                                        join c in dataContext.ProjectImpacts on b.impt_reference equals c.impt_reference
                                        where
                                        c.proj_reference.Contains(project)

                                        select p.cmpt_name).Distinct();
            complist = quary.ToList();

            return complist;

        }


        public List<Impacts> getImpactsFromProjectImpacts(String prefe, String impcName)
        {
            //var innerJoinQuery =
            //from c in cecbContext.ProjectImpacts
            //join d in cecbContext.Impacts on c.impt_reference equals d.impt_reference
            //where c.proj_reference.Contains(prefe) && d.impt_name.Contains(impcName)
            //select new { CustomerName = d.impt_name };
            List<Impacts> SelectedImpacts = null;
            try
            {
                // String xim = dataContext.Impacts.First(i => i.impt_name.Contains(impcName)).impt_reference;
                IQueryable<Impacts> query = from c in dataContext.ProjectImpacts
                                            join b in dataContext.Impacts on c.impt_reference equals b.impt_reference
                                            //  join d in dataContext.LeopoldMatrixValues on b.impt_reference equals d.impt_reference
                                            where c.proj_reference == prefe && b.impt_name.Contains(impcName)
                                            //&& d.mtrx_magnitude == null && d.mtrx_importance == null
                                            select b;




                SelectedImpacts = query.ToList();
            }
            catch (Exception)
            {


            }


            return SelectedImpacts;
        }

        public List<Impacts> getNotExistingImpactsByProjectID(Projects proj)
        {
            IQueryable<Impacts> Query;
            
                Query =
                   from c in dataContext.Impacts
                   where (!dataContext.ProjectImpacts.Any(p => p.impt_reference == c.impt_reference)) && (dataContext.ProjectImpacts.Any(r => r.proj_reference == proj.proj_reference ))
                   select c;
            

            List<Impacts> SelectedComponent = Query.ToList();

            return SelectedComponent;

        }

        public List<ProjectImpacts> getProjectImpactsByProjectID(String impcName)
        {
            IQueryable<ProjectImpacts> Query =
                  from c in dataContext.ProjectImpacts
                  where c.proj_reference.Contains(impcName)

                  select c;

            List<ProjectImpacts> SelectedImpacts = Query.ToList();

            return SelectedImpacts;
        }




        public List<Impacts> getImpactDetailsByName(String name , Projects proj , Boolean isAll)
        {
            IQueryable<Impacts> Query;
            if (isAll)
            {
                  Query =
                from c in dataContext.Impacts
                where  c.impt_name.Contains(name)
                select c;
           
            }
            else
            {
                  Query =
                     from c in dataContext.Impacts
                     where (!dataContext.ProjectImpacts.Any(p => p.impt_reference == c.impt_reference)) && (dataContext.ProjectImpacts.Any(r => r.proj_reference == proj.proj_reference && c.impt_name.Contains(name)))
                     select c;
            }

            List<Impacts> SelectedComponent = Query.ToList();

            return SelectedComponent;

        }

        private String getImpactReference()
        {
            int sub;


            try
            {
                var idKey = (from c in dataContext.Impacts
                             orderby c.impt_id descending
                             select c).First();


                String id = idKey.impt_reference.ToString();

                sub = int.Parse(id.Substring(4)) + 1;
            }
            catch (Exception)
            {
                sub = 1;
            }
            String newId = "IMPC" + sub;
            return newId.ToString();

        }


        Impacts impt;
        public bool InsertImpactsEditedMember()
        {
            bool IsInserted = false;
            try
            {
                String reference = getImpactReference();
                impt = new Impacts();
                impt.impt_reference = reference;
                //      impt.impt_name = name;
                //    impt.Component.cmpt_reference = designation;
                impt.impt_status = true;


                dataContext.AddToImpacts(impt);
                dataContext.SaveChanges();



                IsInserted = true;
            }
            catch (Exception)
            {

                IsInserted = false;
            }


            return IsInserted;
        }

        public List<ProjectImpacts> getProjectImpactDetailsByName(String name, Projects proj)
        {
            IQueryable<ProjectImpacts> query;


            query = from c in dataContext.Impacts
                    join d in dataContext.ProjectImpacts on c.impt_reference equals d.impt_reference
                    where d.proj_reference == proj.proj_reference && c.impt_name.Contains(name)
                    select d;


            List<ProjectImpacts> selectedComponent = query.ToList();

            return selectedComponent;

        }


        public bool InsertImpact(List<String> phase, List<String> act, List<String> refe , int count, String proRef)
        {
            Impacts impac;
            int itemNo = 0;
            bool IsInserted = false;
            try
            {
                while (count > 0)
                {

                     
                    ProjectImpacts pimpact = new ProjectImpacts();
                    pimpact.proj_reference = proRef;
                    pimpact.impt_reference = refe[itemNo];
                    pimpact.impt_addedBy = "Admin";
                    pimpact.impt_approvedBy = "";
                    pimpact.projImpact_status = true;
                    dataContext.AddToProjectImpacts(pimpact);
                    dataContext.SaveChanges();
                    itemNo++;
                    count--;

                }


                IsInserted = true;
            }
            catch (Exception)
            {

                IsInserted = false;
            }


            return IsInserted;
        }


        public bool InsertNewImpact(String impact , String comp , String proRef)
        {
            Impacts impac;
             
            bool IsInserted = false;
            try
            {
                 

                    String reference = getImpactReference();
                    impac = new Impacts();
                    impac.impt_reference = reference;
                    impac.impt_name = impact;
                    impac.impt_component = comp;
                    impac.impt_status = true;
                 
                    dataContext.AddToImpacts(impac);
                    dataContext.SaveChanges();
                 

                IsInserted = true;
            }
            catch (Exception)
            {

                IsInserted = false;
            }


            return IsInserted;
        }


        public String InsertNewImpactReturnRef(String impact, String comp, String proRef)
        {
            Impacts impac;
            String refe = "";
            bool IsInserted = false;
            try
            {


                String reference = getImpactReference();
                refe = reference;
                impac = new Impacts();
                impac.impt_reference = reference;
                impac.impt_name = impact;
                String cname = comp;
                impac.ComponentsReference.Value = dataContext.Components.First(i => i.cmpt_reference == cname);

                impac.impt_status = true;

                dataContext.AddToImpacts(impac);
                dataContext.SaveChanges();




                IsInserted = true;
            }
            catch (Exception)
            {

                IsInserted = false;
            }


            return refe;
        }


        public String findIref(String imp)
        {


            string sub = "";



            try
            {
                var idKey = (from c in dataContext.Impacts
                             where c.impt_name.Contains(imp)
                             select c).First();


                sub = idKey.impt_reference.ToString();


            }
            catch (Exception)
            {

            }

            return sub;


        }


        public String getInpactNameById(String id)
        {

            var idKey = (from c in dataContext.Impacts
                         where c.impt_reference == id
                         select c).First();


            return idKey.impt_name.ToString();

        }

        public List<Impacts> getImpacts(string project, string comp)
        {
            List<Impacts> list = new List<Impacts>();
            IQueryable<Impacts> quary = (from p in dataContext.Impacts
                                         join b in dataContext.ProjectImpacts

                                         on p.impt_reference
                                         equals b.impt_reference
                                         join d in dataContext.Components on p.impt_component equals d.cmpt_reference
                                         where
                                         b.proj_reference.Contains(project) && d.cmpt_name.Contains(comp)

                                         select p);
            list = quary.ToList();


            // (from a in context.Accounts
            // join c in context.Clients on a.UserID equals c.UserID
            // where c.ClientID == yourDescriptionObject.ClientID
            // select a.Balance)
            //.SingleOrDefault();

            //(from c in ce.Campaign.Include("Test")
            // join t in ce.Tests.Include("TestAnswer").Include("User") on c.Test.ID equals t.ID
            // where c.ID == campaignID
            // select c).FirstOrDefault();




            list = quary.ToList();
            return list;
        }
        public bool updateImpactsDetails(string impt_ref, string impact)
        {
            bool isselected = false;
            try
            {

                Impacts impactObject = dataContext.Impacts.First(i => i.impt_reference == impt_ref);

                //    ProjectImpacts projectimpactObject = dataContext.ProjectImpacts.First(i => i.ImpactsReference == impt_ref);

                impactObject.impt_name = impact;


                dataContext.SaveChanges();
                isselected = true;

            }
            catch (Exception)
            {

            }

            return isselected;

        }

        public String findAref(String act)
        {


            string sub = "";



            try
            {
                var idKey = (from c in dataContext.Activities
                             where c.actv_name.Contains(act)
                             select c).First();


                sub = idKey.actv_reference.ToString();


            }
            catch (Exception)
            {

            }

            return sub;


        }


        public bool insertProjectImpacts(String proRef, String actRef)
        { 
            try
            {
                ProjectImpacts pact = new ProjectImpacts();
                pact.proj_reference = proRef;
                pact.impt_reference = actRef;
                pact.impt_addedBy = "";
                pact.impt_approvedBy = "";
               
                dataContext.AddToProjectImpacts(pact);
                dataContext.SaveChanges();
                return true;
            }
            catch (Exception)
            {

                return false;
            }

        }

        public bool deleteProjectImpact(ProjectImpacts projim , Boolean isCompleteRemove) {

            try
            {
                if (isCompleteRemove)
                {
                    deleteFromLeopoldMatrix(projim);
                    deleteFromSimpleMatrix(projim);
                    deleteImpact(projim);
                }
                else
                {
                    ProjectImpacts pim = dataContext.ProjectImpacts.First(i => i.impt_reference.Equals(projim.impt_reference) && i.proj_reference.Equals(projim.proj_reference));
                    dataContext.ProjectImpacts.DeleteObject(pim);
                    dataContext.SaveChanges();
                }
                return true;
            }
            catch (System.Data.UpdateException ex)
            {

               
                
                return false;


            }
        
        }

        private void deleteFromLeopoldMatrix(ProjectImpacts projim )
        {
            List<LeopoldMatrixValues> leo = (from c in dataContext.LeopoldMatrixValues
                                             where c.proj_reference.Equals(projim.proj_reference) && c.impt_reference.Equals(projim.impt_reference)
                                             select c).ToList();
            foreach (LeopoldMatrixValues l in leo)
            {

                dataContext.LeopoldMatrixValues.DeleteObject(l);
                dataContext.SaveChanges();
            }

            List<LeopoldMatrixValuesExtra> leox = (from c in dataContext.LeopoldMatrixValuesExtra
                                                   where c.proj_reference.Equals(projim.proj_reference) && c.impt_reference.Equals(projim.impt_reference)
                                                   select c).ToList();
            foreach (LeopoldMatrixValuesExtra l in leox)
            {

                dataContext.LeopoldMatrixValuesExtra.DeleteObject(l);
                dataContext.SaveChanges();
            }

             
        }

        private void deleteFromSimpleMatrix(ProjectImpacts projim )
        {
            List<SimpleMatrixValues> sim = (from c in dataContext.SimpleMatrixValues
                                             where c.proj_reference.Equals(projim.proj_reference) && c.impt_reference.Equals(projim.impt_reference)
                                             select c).ToList();
            foreach (SimpleMatrixValues l in sim)
            {

                dataContext.SimpleMatrixValues.DeleteObject(l);
                dataContext.SaveChanges();
            }

            List<SimpleMatrixValuesExtra> simx = (from c in dataContext.SimpleMatrixValuesExtra
                                                   where c.proj_reference.Equals(projim.proj_reference) && c.impt_reference.Equals(projim.impt_reference)
                                                   select c).ToList();
            foreach (SimpleMatrixValuesExtra l in simx)
            {

                dataContext.SimpleMatrixValuesExtra.DeleteObject(l);
                dataContext.SaveChanges();
            }

            
        }

        private void deleteImpact(ProjectImpacts projim )
        {
            ProjectImpacts proImp = dataContext.ProjectImpacts.First(i => i.impt_reference.Equals(projim.impt_reference) && i.proj_reference.Equals(projim.proj_reference));
            dataContext.ProjectImpacts.DeleteObject(proImp);
            dataContext.SaveChanges();
        }

        public Boolean checkImpactIsAllowedToDelete(ProjectImpacts proj)
        {

            Projects val = dataContext.Projects.First(c => c.proj_reference.Equals(proj.proj_reference));
                         
            if (val.proj_isEmailsSend == false)
                return true;
            else
                return false;

        }
    }
}
